﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.YHP_TransManage.Test
{
    public partial class 菱声接口版音响测试 : Form
    {
        public 菱声接口版音响测试()
        {
            InitializeComponent();
        }
        TcpClient  tcpClient = new TcpClient();
        private async void button1_Click(object sender, EventArgs e)
        {
            // byte ipbyte = Convert.ToByte("192.168.3.27");
            //var aa =  new IPEndPoint(IPAddress.Parse("192.168.3.27"), 9501);
            //System.Net.IPAddress iPAddress = new System.Net.IPAddress(ipbyte,);
            if (!tcpClient.Connected)
            {
                await tcpClient.ConnectAsync(IPAddress.Parse("192.168.3.27"), 9501);
                Common.ShowBlackBox.WriteLine("1 客户端正在创建");
            }
            if (tcpClient.Connected)
            {
                OpenFileDialog open_file = new OpenFileDialog();
                open_file.Filter = "bin files (*.mp3)|*.mp3";
                string s_file = "";
                if (open_file.ShowDialog() == DialogResult.OK)
                {
                    s_file = open_file.FileName;
                }
                Common.ShowBlackBox.WriteLine("1 connection is success");
                string str_play = "28|";
                string str_p = "Manager-" + "PlayMP3File-" + s_file + @"/*/" + str_play + "-DataEnd\r";
                byte[] buf = System.Text.Encoding.Default.GetBytes(str_p);
                var sendmmm = new SocketAsyncEventArgs();
                //sendmmm.AcceptSocket = tcpClient.Client;
                sendmmm.SetBuffer(buf, 0, buf.Length);
                sendmmm.Completed += Sendmmm_Completed;
                tcpClient.Client.SendAsync(sendmmm);
                return;
            }
            Common.ShowBlackBox.WriteLine("2 connection is failt");
        }

        private void Sendmmm_Completed(object sender, SocketAsyncEventArgs e)
        {
            Common.ShowBlackBox.WriteLine("1 数据发送成功");
        }

        private void ConnectCallback(IAsyncResult ar)
        {
            if (tcpClient.Connected)
            {
                Common.ShowBlackBox.WriteLine("1 connection is success");
                return;
            }
            Common.ShowBlackBox.WriteLine("1 connection is failt");

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
